import axios, { type AxiosResponse } from 'axios';
import { ElMessage, ElMessageBox } from 'element-plus';

/* 定义response对象的data接口 */
interface ResponseData<T> {
    code: number;
    data: T;
    message: string;
}

// 配置新建一个 axios 实例
const service = axios.create({
    baseURL: '/mock', 
    timeout: 1000,
});

// 添加请求拦截器
service.interceptors.request.use(
    (config) => {
      return config;
    }
  );

  // 添加响应拦截器
service.interceptors.response.use(
    async (response: AxiosResponse<ResponseData<any>>) => {
      const res = response.data; 
      return res.data
    },
    (error) => {
      ElMessage.error('请求失败了...')
      return Promise.reject(error);
    }
  );
  
  export default service;